<template>
    <div>
        <List v-model="loading" :finished="finished" @load="loadMore" finished-text="没有更多数据了" :offset="20">
            <div class="goods-list">
                <div class="goods-item" v-for="(item, i) in goodsList" :key="item.id" @click="goGoodsInfo(item.id)">
                    <div class="goods">
                        <div class="image" v-if="imageList" v-lazy:background-image="imageList[(i % imageList.length)].url"></div>
                        <div class="image" v-else></div>
                        <div class="title">{{ item.title }}</div>
                        <div class="footer">
                            <div class="price">
                                <span class="sell">￥{{ item.sell_price }}</span>
                                <span class="market">{{ item.market_price }}</span>
                            </div>
                            <div class="info">
                                <span>热卖中</span>
                                <span>剩余{{ item.stock_quantity }}件</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </List>
    </div>
</template>

<script>
import Vue from 'vue';
import { Lazyload, List } from 'vant';
import { getGoodsList, getImageList } from '../../services';

Vue.use(Lazyload);

export default {
    components: { List },
    data() {
        return {
            loading: true,
            finished: false,
            page: 1,
            goodsList: null,
            imageList: null,
        };
    },
    methods: {
        // 加载更多
        loadMore() {
            getGoodsList(this.page + 1)
                .then(({ goodsList, page }) => {
                    if (goodsList && goodsList.length > 0) {
                        goodsList.forEach(item => this.goodsList.push(item));
                        this.page = page;
                        this.loading = false;
                    } else {
                        // 没有更多数据了
                        this.loading = false;
                        this.finished = true;
                    }
                })
                .catch(err => console.log(`第${this.page + 1}页商品列表获取失败`, err.message));
        },
        // 使用编程式路由前往 GoodsInfo 页面
        goGoodsInfo(id) {
            this.$router.push({ name: 'GoodsInfo', params: { id } });
        }
    },
    created() {
        getGoodsList()
            .then(({ goodsList, page }) => {
                this.goodsList = goodsList;
                this.page = page;
                this.loading = false;
            })
            .catch(err => console.log('商品列表获取失败', err.message));
        getImageList(11231).then(imageList => this.imageList = imageList);
    }
}
</script>

<style lang="less" scoped>
.goods-list {
    width: 100%;
    padding: 0 1%;
    margin-bottom: 10px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    .goods-item {
        width: 50%;
        padding: 0 1%;
        margin-top: 10px;
        box-sizing: border-box;
        overflow: hidden;
        .goods {
            border-radius: 5px;
            overflow: hidden;
        }
        .image {
            width: 100%;
            padding-top: 150%;
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
        }
        .title {
            font-size: 14px;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            display: -webkit-box;
            overflow: hidden;
        }
        .footer {
            font-size: 12px;
            .price {
                .sell {
                    color: red;
                    font-weight: bold;
                    margin-right: 10px;
                    font-size: 14px;
                    text-decoration: none;
                }
                .market {
                    text-decoration: line-through;
                }
            }
            .info {
                display: flex;
                justify-content: space-between;
                color: #999;
            }
        }
    }
}
</style>